#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>


const double TWO_PI = 6.2831853;
GLsizei winWidth = 400, winHeight = 400;
GLuint regHex;

class screenPt {
private:
	GLint x, y;

public:
	screenPt() {
		x = y = 0;
	}

	void setCoords(GLint xCoord, GLint yCoord) {
		x = xCoord;
		y = yCoord;
	}

	GLint getx() const {
		return x;
	}

	GLint gety() const {  // const不对非const成员变量进行修改
		return y;
	}
};

// static保证只有本代码文件会调用该函数
static void init(void)
{
	screenPt hexVertex, circCtr;
	GLdouble theta;
	GLint k;

	circCtr.setCoords(winWidth / 2, winHeight / 2);
	glClearColor(1.0, 1.0, 1.0, 0.0);	// 设置窗口显示颜色为白色
	regHex = glGenLists(1);
	glNewList(regHex, GL_COMPILE);
		glColor3f(1.0, 0.0, 0.0);
		glBegin(GL_POLYGON);
			for (k = 0; k < 6; k++) {
				theta = TWO_PI * k / 6.0;
				hexVertex.setCoords(circCtr.getx() + 150 * cos(theta), circCtr.gety() + 150 * sin(theta));
				glVertex2i(hexVertex.getx(), hexVertex.gety());
			}
		glEnd();
	glEndList();
}

void regHexagon(void)
{
	glClear(GL_COLOR_BUFFER_BIT);	// 清空窗口显示
	glCallList(regHex);
	glFlush();
}

void winReshapeFcn(int newWidth, int newHeight)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();	// 恢复坐标系
	gluOrtho2D(0.0, (GLdouble)newWidth, 0.0, (GLdouble)newHeight);
	glClear(GL_COLOR_BUFFER_BIT);
}


void main(int argc, char** argv)
{
	glutInit(&argc, argv);								// 初始化glut（可不写默认）
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);		// 显示模式初始化 单缓冲模式 RGB显示模式
	glutInitWindowPosition(100, 100);					// 窗口距离左和上的距离（可不写默认）
	glutInitWindowSize(winWidth, winHeight);			// 窗口的宽和高（可不写默认）
	glutCreateWindow("Reshape Display-List Example");	// 窗口名称

	init();												// 执行初始化程序
	glutDisplayFunc(regHexagon);						// 自定义函数绘图
	glutReshapeFunc(winReshapeFcn);						// 窗口修改后显示
	glutMainLoop();										// 消息循环响应
}
